#
# Utils Makefile
# - In charge of building the utils as library, as well as smoke building the examples.
#
#
# params
CC=gcc
CXX=g++
GDIR=..
EXTRADEFS=-DLINUX_OS $(CE)
INCLUDES=`xml2-config --cflags`
WFLAGSC=-Wall -Wunused -Wshadow
WFLAGSCPP=-Wall -Wunused -Wshadow -Weffc++
WFLAGSDCPP=
CFLAGS=-O0 -g3 -fPIC $(WFLAGSC) $(INCLUDES) $(EXTRADEFS)
CPPFLAGS=-O0 -g3 -fPIC $(WFLAGSCPP) $(INCLUDES) $(EXTRADEFS)
DCPPFLAGS=-O0 -g3 -fPIC $(WFLAGSDCPP) $(INCLUDES) $(EXTRADEFS)
LD=g++
LDFLAGS=
LIBS=-lrt -lpthread `xml2-config --libs`
VPATH=
INCLUDE_EXPORT=../include

# Define COMPILE and LINK commands
COMPILEC=$(CC) $(CFLAGS) -c
COMPILECPP=$(CXX) $(CPPFLAGS) -c
COMPILEDCPP=$(CXX) $(DCPPFLAGS) -c
LINK=$(LD) $(LDFLAGS)

.PHONY: all

# Define targets
TARGETS=examples libGIMutils.a libGIMutils.include

# All rule compiles all per default
all: $(TARGETS)

###############################################
###############################################
.PHONY: libGIMutils.include
libGIMutils.include: 
	mkdir -p $(INCLUDE_EXPORT)/utils
	cp -v base64.hpp bitestream.h crc16.h demangler.hpp ownutils.h	\
        settings.hpp sync.hpp binbag.h bitOps.h databacklog.hpp		\
        gimutils.h owndebug.h thread.hpp $(INCLUDE_EXPORT)/utils 

##############################################
LIBGIMUTILS_SRCS_C = owndebug.c ownutils.c bitestream.c bitOps.c	\
                       crc16.c
LIBGIMUTILS_SRCS_CPP = base64.cpp binbag.cpp demangler.cpp	\
                       gimutils.cpp settings.cpp
LIBGIMUTILS_OBJS = $(LIBGIMUTILS_SRCS_C:.c=.o)		\
                   $(LIBGIMUTILS_SRCS_CPP:.cpp=.opp)
libGIMutils.a: $(LIBGIMUTILS_OBJS)
	@ar rv $@ $?
	@echo "      [AR] $@"
	@ranlib $@
	@echo "      [RANLIB] $@"
###############################################

.libGIMutils.a-dist.marker: libGIMutils.a
	@echo "      [STRIP] libGIMutils.a"
	@strip --strip-debug libGIMutils.a
	@if test "`uname -m`" = "x86_64"; \
	   then echo "64bit architecture detected"; cp -v libGIMutils.a libGIMutils64.a; cp -v libGIMutils.a $(GDIR)/lib64/; \
           else echo "32bit architecture detected"; cp -v libGIMutils.a libGIMutils32.a; cp -v libGIMutils.a $(GDIR)/lib32/; \
	 fi

.PHONY: libGIMutils.a-dist
libGIMutils.a-dist: .libGIMutils.a-dist.marker

###############################################

.PHONY: local-clean clean real-clean realclean
local-clean clean real-clean realclean: examples_clean
	@rm -f *.o *.opp *.dopp libGIMutils* 
	@echo "     [CLEAN]"

########### Compile rules ####################
# Compile rule for all .o (.c) file
%.o:	%.c
	@$(COMPILEC) -o $@ $<
	@echo "      [CC] $<"

# Compile rule for all .opp (.cpp) files
%.opp:	%.cpp
	@$(COMPILECPP) -o $@ $<
	@echo "      [CXX] $<"

# Compile rule for all .dopp (dirty .cpp) files
%.dopp:	%.cpp
	@$(COMPILEDCPP) -o $@ $<
	@echo "      [DIRTY-CXX] $<"

##############################################
########## Subdirectory rules ################

examples: force_look
	echo "Entering examples"
	+cd examples; make all

examples_clean: force_look
	+cd examples; make real-clean

force_look:
	true


##############################################
##############################################
